﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Mail;

using System.Collections.Generic;
using Jzy.Model.Mail;
using Jzy.Model;
using Frame.Lib.Common;
using Frame.Lib.Core.Module;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Attach;
using Jzy.DAL;


public partial class Mail_Detail : PageDetail
{
    private MailBO _mail;
    private MailFolderBO _mailBox;

    private MailUserBO MailUser
    {
        get
        {
            return MailUserBO.Instance<MailUserBO>();
        }
    }

    private int FolderID
    {
        get
        {
            return int.Parse(Request["FolderID"]);
        }
    }

    private string MailUserID
    {
        get
        {
            return Request["MailUserID"];
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (FolderID == (int)MailSysFolder.草稿箱)
        {
            Response.Redirect("Edit.aspx?MailUserID=" + MailUserID + "&Mode=" + (int)WriteMailMode.Draft, true);
        }
        _mail = new MailBO(AppSession.UserID);
        _mailBox = new MailFolderBO(AppSession.UserID, MailFolderType.内部邮件);

        if (!IsPostBack)
        {
            BindMenu();
            DispInfo();
            SetRead();
        }
        AddAttr();

        int unreadMailCount = _mail.GetUnreadMailCount();
        ModuleBO module = new ModuleBO();

        object receiveBoxModuleID = module.GetIDByCode(ModuleCode.ReceiveBox);
        if (receiveBoxModuleID != null)
        {
            ModuleInfo info = module.GetInfo(receiveBoxModuleID);
            //SetModuleName(id,name)
            string strScript = "SetModuleName('" + receiveBoxModuleID + "','" + info.Name + "');";
            if (unreadMailCount > 0)
            {
                strScript = "SetModuleName('" + receiveBoxModuleID + "','<span style=\\'font-weight:bold\\'>" + info.Name + "(" + unreadMailCount + ")</span>');";
            }
            JSUtil.ExcuteJS(strScript, "UnreadMail");
        }
        this.AllowPageScroll();
    }

    private void AddAttr()
    {
        btnReply.Style.Add("display", "none");
        btnForword.Style.Add("display", "none");
        btnDelete.Style.Add("display", "none");
        btnDelete.Attributes.Add("onclick", "return confirm('确认删除邮件');");
        JSUtil.ClickBtn(lnkReply, btnReply);
        JSUtil.ClickBtn(lnkForward, btnForword);
        JSUtil.ClickBtn(lnkDelete, btnDelete);
    }

    private void SetRead()
    {
        _mail.SetRead(MailUserID);
    }

    private void DispInfo()
    {
        int mailID = _mail.GetMailID(MailUserID);
        MailInfo info = _mail.GetInfo(mailID);       

        EntityUtil.SetControl(pnlMain, info);

        if (info.CopyToNames == null || info.CopyToNames == "")
        {
            trCopyTo.Style["display"] = "none";
        }

        //显示附件
        AttachBO attach = new AttachBO(OTableName.MailAttach);
        IEnumerable<AttachInfo> list = attach.GetList(mailID);
        rptAttach.DataSource = list;
        rptAttach.DataBind();

        if (list.Count() == 0)
        {
            trAttach.Style["display"] = "none";
        }

        //处理已读回执
        MailUserInfo mailUserInfo = (new MailUserBO()).GetInfo(MailUserID);
        if (mailUserInfo.Status == (int)MailStatus.UnRead && info.AskReceipt == true)
        {
            string script = "sendReceipt(" + AppSession.UserID + ",'" + Server.UrlEncode(MailUserID) + "');";
            JSUtil.ExcuteJS(script, "sendReceipt");
        }

    }
    protected void rptMenu_ItemCommand(object source, RepeaterCommandEventArgs e)
    {      
        _mail.Move(MailUserID, e.CommandArgument);
        Response.Redirect("List.aspx?FolderID=" + FolderID, true);      
    }

    private void BindMenu()
    {
        List<MailFolderInfo> list = _mailBox.GetAllBoxList();
        rptMenu.DataSource = list;
        rptMenu.DataBind();
    }

    protected void btnReply_Click(object sender, EventArgs e)
    {
        Response.Redirect("Edit.aspx?MailUserID=" + MailUserID + "&Mode=" + (int)WriteMailMode.Reply, true);
    }
    protected void btnForword_Click(object sender, EventArgs e)
    {
        Response.Redirect("Edit.aspx?MailUserID=" + MailUserID + "&Mode=" + (int)WriteMailMode.Forword, true);
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (FolderID == (int)MailSysFolder.已删除)
        {
            _mail.DeleteBoxMail(MailUserID);
        }
        else
        {
            _mail.Move(MailUserID, (int)MailSysFolder.已删除);
        }

        Response.Redirect("List.aspx?FolderID=" + FolderID, true);
    }
}
